package com.mycompany.customContainer;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;


import com.mycompany.model.User;
import com.mycompany.service.UserService;
import com.mycompany.ui.AppUI;
import com.vaadin.data.util.BeanContainer;
import com.vaadin.data.util.BeanItem;
import com.vaadin.server.WrappedHttpSession;
import com.vaadin.server.WrappedSession;
import com.vaadin.ui.Component;
import com.vaadin.ui.UI;


public class LazyLoadedContainer extends BeanContainer {

	private UserService userService;
	private ApplicationContext applicationContext;
	
	public LazyLoadedContainer(Class type) {
		super(type);
		userService = getUserService();
		System.out.println(userService);
		
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public int size() {
		// TODO Auto-generated method stub
		System.out.println("Size "+userService.selectAll().size());
		return userService.selectAll().size();
	}
	
	@Override
	public BeanItem getItem(Object itemId) {
		// TODO Auto-generated method stub
		User user = (User) itemId;
		System.out.println(user);
		return new BeanItem<User> (user);
	}
	
	  @Override
	public List<User> getItemIds(int startIndex, int numerOfIds ) {
		 
		 //int endIndex = startIndex+numerOfIds;
		 List<User> users = userService.findByCognome(startIndex, numerOfIds);
		 return users;
		// TODO Auto-generated method stub
	}
	  
	 
	  
	private UserService getUserService(){
		AppUI current = (AppUI) (UI.getCurrent());
		ApplicationContext context = current.getApplicationContext();
		return  context.getBean(UserService.class);
	}

}
